var router = require('express').Router();
const url = require("url");
import jwt from "../controller/util/jwt"
import Logs from "../controller/util/logs";
import mongo from "../controller/util/mongodb";
//select db
/*
you can have a http
/?name=xxx&_id=xxx
/?name=xxx
/?_id=xxx
*/
router.get('/', (req, res, next) => {
    let arg = url.parse(req.url, true).query;
    let { name, id } = arg;
    if (name != undefined && id != undefined) {
        mongo.find({ "$and": [{ "_id": id, "name": name }] }).then((result) => {
            res.json(result)
        }).catch((err) => {
            Logs.catchError(err);
            res.json(err)
        });
    } else if (name != undefined) {
        mongo.find({ "name": name }).then((result) => {
            res.json(result)
        }).catch((err) => {
            Logs.catchError(err);
            res.json(err)
        });
    } else if (id != undefined) {
        mongo.find({ "_id": id }).then((result) => {
            res.json(result)
        }).catch((err) => {
            Logs.catchError(err);
            res.json(err)
        });
    } else {
        res.json("Missing parameter")
    }
});

//update db
/**
 * /?_id=xxx&name=xxx&newname=xxx&description=xxx
 */
router.post('/', (req, res, next) => {
    let arg = url.parse(req.url, true).query;
    let { name, id, newname, description } = arg;
    if (name != undefined && id != undefined) {
        let query = { "$and": [{ "_id": id, "name": name }] };
        if (newname != undefined && description != undefined) {
            mongo.update(query, { "name": newname, "description": description }).then((result) => {
                res.json(result)
            }).catch((err) => {
                Logs.catchError(err);
                res.json(err)
            });
        } else if (newname != undefined) {
            mongo.update(query, { "name": newname }).then((result) => {
                res.json(result)
            }).catch((err) => {
                Logs.catchError(err);
                res.json(err)
            });
        } else if (description != undefined) {
            mongo.update(query, { "description": description }).then((result) => {
                res.json(result)
            }).catch((err) => {
                Logs.catchError(err);
                res.json(err)
            });
        }
    } else if (name != undefined) {
        let query = { "name": name };
        if (newname != undefined && description != undefined) {
            mongo.update(query, { "name": newname, "description": description }).then((result) => {
                res.json(result)
            }).catch((err) => {
                Logs.catchError(err);
                res.json(err)
            });
        } else if (newname != undefined) {
            mongo.update(query, { "name": newname }).then((result) => {
                res.json(result)
            }).catch((err) => {
                Logs.catchError(err);
                res.json(err)
            });
        } else if (description != undefined) {
            mongo.update(query, { "description": description }).then((result) => {
                res.json(result)
            }).catch((err) => {
                Logs.catchError(err);
                res.json(err)
            });
        }
    } else if (id != undefined) {
        let query = { "_id": id };
        if (newname != undefined && description != undefined) {
            mongo.update(query, { "name": newname, "description": description }).then((result) => {
                res.json(result)
            }).catch((err) => {
                Logs.catchError(err);
                res.json(err)
            });
        } else if (newname != undefined) {
            mongo.update(query, { "name": newname }).then((result) => {
                res.json(result)
            }).catch((err) => {
                Logs.catchError(err);
                res.json(err)
            });
        } else if (description != undefined) {
            mongo.update(query, { "description": description }).then((result) => {
                res.json(result)
            }).catch((err) => {
                Logs.catchError(err);
                res.json(err)
            });
        }
    } else {
        res.json("Missing parameter")
    }
});
//delete db
/**
 * /?_id=xxx&name=xxx
 */
router.delete('/', (req, res, next) => {
    let arg = url.parse(req.url, true).query;
    let { name, id } = arg;
    if (name != undefined && id != undefined) {
        mongo.delete({ "$and": [{ "_id": id, "name": name }] }).then((result) => {
            res.json(result)
        }).catch((err) => {
            Logs.catchError(err);
            res.json(err)
        });
    } else if (name != undefined) {
        mongo.delete({ "name": name }).then((result) => {
            res.json(result)
        }).catch((err) => {
            Logs.catchError(err);
            res.json(err)
        });
    } else if (id != undefined) {
        mongo.delete({ "_id": id }).then((result) => {
            res.json(result)
        }).catch((err) => {
            Logs.catchError(err);
            res.json(err)
        });
    } else {
        res.json("Missing parameter")
    }
});
//insert db
/**
 * /?name=xxx&description=xxx&dob=xxx
 */
router.put('/', (req, res) => {
    let arg = url.parse(req.url, true).query;
    let { name, description, dob } = arg;
    if (name != undefined && description != undefined && dob != undefined) {
        mongo.save({ "_id": name, "name": name, "description": description, "dob": dob }).then((result) => {
            res.json(result)
        }).catch((err) => {
            Logs.catchError(err);
            res.json(err)
        });
    } else if (name != undefined && description != undefined) {
        mongo.save({ "_id": name, "description": description, "name": name }).then((result) => {
            res.json(result)
        }).catch((err) => {
            Logs.catchError(err);
            res.json(err)
        });
    } else if (name != undefined && dob != undefined) {
        mongo.save({ "_id": name, "name": name, "dob": dob }).then((result) => {
            res.json(result)
        }).catch((err) => {
            Logs.catchError(err);
            res.json(err)
        });
    } else if (name != undefined) {
        mongo.save({ "_id": name, "name": name }).then((result) => {
            res.json(result)
        }).catch((err) => {
            Logs.catchError(err);
            res.json(err)
        });
    } else {
        res.json("Missing parameter")
    }
});

module.exports = router;